$(document).ready(function(){
    var pay=[];
    var remarks=[];
    var schoolid,collegeid,majorid,paymentMethod,paymentPlace;
    /*先判断用户是否在线*/
    $.ajax({
        url:interfaceGetState+generateMixed(9),
        type:'post',
        success:function (data) {
            if(data.status==0){
                //已登录
                /*是否实名*/
                realName();
                reset();
                $.ajax({
                    url:interfaceGSESchool+generateMixed(9),
                    type:'post',
                    success:function(data){
                        // console.log(data);
                        for(var i=0;i<data.length;i++){
                            $("#School-for-examination").append("<option value="+data[i].id+">"+data[i].name+"</option>");
                        }
                        collegeForExamination(data[0].id);
                        schoolid=data[0].id;
                    }
                })
            }else {
                //未登录
                alert("请先登录");
                if(window.sessionStorage){
                    var url=window.location.href;
                    sessionStorage.setItem("prePage",url);
                }
                window.location.href="/f/campusinfo/login"+generateMixed(9);
            }
        }
    });

    
    /*报考学校的选择事件*/
    $("#School-for-examination").change(function () {
        $("#major").empty();
        reset();
       var id=$("#School-for-examination").val();
       collegeForExamination(id);
        schoolid=id;
    });
    /*报考学院的选择事件*/
    $("#College-for-examination").change(function () {
        reset();
        var id=$("#College-for-examination").val();
        major(id);
        collegeid=id;
    })
    /*报考学院*/
    function collegeForExamination(id) {
        $("#College-for-examination").empty();
        $.ajax({
            url:interfaceGSECollege+generateMixed(9),
            type:'post',
            data:{schoolid:id},
            success:function(data){
                // console.log(data);
                if(data.length>0){
                    for(var i=0;i<data.length;i++){
                        $("#College-for-examination").append("<option value="+data[i].id+">"+data[i].name+"</option>");
                    }
                    major(data[0].id);
                    collegeid=data[0].id;
                }
            }
        });
    }
    /*报考专业*/
    function major(id) {
        reset();
        $("#major").empty();
        $.ajax({
            url:interfaceGSEMajor+generateMixed(9),
            type:'post',
            data:{collegeid:id},
            success:function(data){
                // console.log(data);
                for(var i=0;i<data.length;i++){
                    $("#major").append("<option number="+i+" value="+data[i].id+">"+data[i].name+"</option>");
                    pay[i]=data[i].payment;
                    remarks[i]=data[i].unLinepayment;
                }
                $("#price").val(pay[0]);
                $("#address").html(remarks[0]);
                majorid=data[0].id;
            }
        });
    }
    /*专业改变事件*/
    $("#major").change(function(){
        pay=[];
        remarks=[];
        var number=$("#remarks>option:selected").attr("number");
        majorid= $("#major").val();
        $("#price").html(pay[number]);
        $("#address").html(remarks[number]);
        paymentPlace=remarks[number];
    })
    /*考研报名提交*/
    $("#Graduate-school-exam-btn").click(function () {
        /*先判断用户是否在线*/
        $.ajax({
            url:interfaceGetState+generateMixed(9),
            type:'post',
            success:function (data) {
                if(data.status==0){
                    //已登录
                    /*是否实名*/
                   realName();
                    if(real){   //已实名
                        $.ajax({
                            url:interfaceAddPubme+generateMixed(9),
                            type:'post',
                            data:{schoolid:schoolid,collegeid:collegeid,majorid:majorid,paymentMethod:1,paymentPlace:paymentPlace},
                            success:function (data) {
                                // console.log(data);
                                if(data.success){
                                    alert(data.message);
                                    window.history.go(-1);
                                }else {
                                    alert(data.message);
                                }
                            }
                        })
                    }else{      //未实名
                        alert("未实名，请完善个人信息")
                    }
                }else {
                    //未登录
                    alert("请先登录");
                    if(window.sessionStorage){
                        var url=window.location.href;
                        sessionStorage.setItem("prePage",url);
                    }
                    window.location.href="/f/campusinfo/login"+generateMixed(9);
                }
            }
        });
    })
    function reset(){
        $("#price").val("");
        $("#address").html("");
    }
})